package com.niit.utils;

import java.sql.*;
import java.time.LocalDate;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class UserDataGenerator {
    private static final String[] PHONE_PREFIXES = {"130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
            "150", "151", "152", "153", "155", "156", "157", "158", "159",
            "180", "181", "182", "183", "184", "185", "186", "187", "188", "189"};

    public static void generateUsers(Connection connection) throws SQLException {
        Random random = new Random();
        String insertSQL = "INSERT INTO users (phone_num, password, gender, birthday, create_time, role) VALUES (?, ?, ?, ?, ?, ?)";

        try (PreparedStatement pstmt = connection.prepareStatement(insertSQL)) {
            for (int i = 0; i < 1000; i++) {
                // Generate phone number
                String phonePrefix = PHONE_PREFIXES[random.nextInt(PHONE_PREFIXES.length)];
                String phoneSuffix = String.format("%08d", random.nextInt(100000000));
                String phoneNum = phonePrefix + phoneSuffix;

                // Generate password (simplified - in real app use proper hashing)
                String password = "password" + random.nextInt(1000);

                // Generate gender (m or f, with 10% chance of null)
                String gender = random.nextInt(10) < 1 ? null : (random.nextBoolean() ? "m" : "f");

                // Generate birthday (between 18 and 80 years ago)
                LocalDate birthday = generateRandomDate(
                        LocalDate.now().minusYears(80),
                        LocalDate.now().minusYears(18)
                );

                // Generate create time (between 1 year ago and now)
                LocalDate createTime = generateRandomDate(
                        LocalDate.now().minusYears(1),
                        LocalDate.now()
                );

                // Generate role (90% chance of 1, 9% chance of 2, 1% chance of 3)
                int roleRand = random.nextInt(100);
                int role = roleRand < 90 ? 1 : (roleRand < 99 ? 2 : 3);

                // Set parameters
                pstmt.setString(1, phoneNum);
                pstmt.setString(2, password);
                if (gender == null) {
                    pstmt.setNull(3, Types.CHAR);
                } else {
                    pstmt.setString(3, gender);
                }
                pstmt.setDate(4, Date.valueOf(birthday));
                pstmt.setDate(5, Date.valueOf(createTime));
                pstmt.setInt(6, role);

                pstmt.addBatch();

                // Execute batch every 100 records
                if (i % 100 == 0) {
                    pstmt.executeBatch();
                }
            }
            // Execute remaining batch
            pstmt.executeBatch();
        }
    }

    private static LocalDate generateRandomDate(LocalDate startInclusive, LocalDate endExclusive) {
        long startEpochDay = startInclusive.toEpochDay();
        long endEpochDay = endExclusive.toEpochDay();
        long randomDay = ThreadLocalRandom.current().nextLong(startEpochDay, endEpochDay);
        return LocalDate.ofEpochDay(randomDay);
    }
}
